<?php

namespace App\Http\Controllers\Script;

use App\Http\Controllers\Controller;
use App\Models\CzProductBlack;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    // 产品限充
    public function addProductLimit()
    {
        // $mobiles = CzProductBlack::where('spec_id', 12)->where('product_id', 7)->where('status', 1)->pluck('mobile')->toArray();
        // dd(in_array('18379467634', $mobiles));
        // return $mobiles;
        $res1 = DB::statement("alter table `cz_spec` add `is_limit` tinyint(1) DEFAULT 0 COMMENT '是否开启限充';");
        $res2 = DB::statement("alter table `cz_spec` add `limit_time` int(11) DEFAULT NULL COMMENT '限充开始时间';");

        return success(['data' => [
            $res1,
            $res2
        ]]);
    }
    // 渠道添加加款字段
    public function addChannelMoney()
    {
        $res1 = DB::statement("alter table `cz_channel` add `money` decimal(11,2) DEFAULT 0 COMMENT '加款金额';");
        // $res1 = DB::statement("alter table `sys_users` drop column `is_voucher`;");

        return success(['data' => [
            $res1
        ]]);
    }
    // 添加客户下单的手机号一天的上限
    public function addUserPhoneMax()
    {
        $res1 = DB::statement("alter table `sys_users` add `phone_max` int(11) DEFAULT 5 COMMENT '手机号一天的提单次数';");
        // $res1 = DB::statement("alter table `sys_users` drop column `is_voucher`;");

        return success(['data' => [
            $res1
        ]]);
    }
    public function addVoucher()
    {

        $res1 = DB::statement("alter table `cz_order` add `voucher` varchar(255) DEFAULT NULL COMMENT '充值凭证';");
        // $res1 = DB::statement("alter table `cz_order` drop column `voucher`;");

        return success(['data' => [
            $res1
        ]]);
    }
    public function addUserVoucher()
    {
        $res1 = DB::statement("alter table `sys_users` add `is_voucher` tinyint(1) DEFAULT 0 COMMENT '是否透传0否1是';");
        // $res1 = DB::statement("alter table `sys_users` drop column `is_voucher`;");

        return success(['data' => [
            $res1
        ]]);
    }
    // 添加渠道字段
    public function addChannelCol()
    {
        $res1 = DB::statement("alter table cz_channel add balance DECIMAL(10,2) DEFAULT NULL COMMENT '渠道余额' AFTER is_sync;");
        $res2 = DB::statement("alter table cz_channel add credit DECIMAL(10,2) DEFAULT NULL COMMENT '渠道授信' AFTER balance;");
        $res3 = DB::statement("alter table cz_channel add msg varchar(255) DEFAULT NULL AFTER credit;");
        $res4 = DB::statement("alter table cz_channel add backParams text DEFAULT NULL COMMENT '返回参数' AFTER msg;");
        return success(['data' => [
            $res1, $res2, $res3, $res4
        ]]);
    }
    // 订单表中添加无限重提限制
    public function addOrderCol()
    {
        $res1 = DB::statement("ALTER TABLE `cz_order` ADD COLUMN `submit_order_count` int(11) NULL DEFAULT 50 COMMENT '无限重提的订单总次数' AFTER `callback_url`;");
        $res2 = DB::statement("ALTER TABLE `cz_order` ADD COLUMN `cur_submit_order_count` int(11) NULL DEFAULT 0 COMMENT '当前重提的订单次数' AFTER `callback_url`;");
        $res3 = DB::statement("ALTER TABLE `cz_customer_product` ADD COLUMN `submit_order_count` int(11) NULL DEFAULT 50 COMMENT '无限重提的订单总次数' AFTER `update_by`;");
        $res4 = DB::statement("ALTER TABLE `cz_user_group_product`  ADD COLUMN `submit_order_count` int(11) NULL DEFAULT 50 COMMENT '无限重提的订单总次数' AFTER `update_by`;");


        return success(['data' => [
            $res1, $res2, $res3, $res4
        ]]);
    }
    //
    public function downZipFile()
    {
        //压缩文件
        $url = "http://cz.wwbest.cn/admin.tar.gz";
        $save_dir = public_path();
        //使用默认名字
        $filename = basename($url);
        if (is_file($save_dir . '/' . $filename)) {
            echo '已存在文件名为' . $filename . '的文件，将会覆盖';
        }
        //下载文件到本地服务器
        $res = $this->getFile($url, $save_dir, $filename, 1);
        if (is_file($filename)) {
            $message = shell_exec("tar -zxvf {$filename};");
            $message = shell_exec("unzip {$filename} -d {$save_dir};");
            $fileSize = ($res['file_size'] / 1024 / 1024) . 'M';
            return "<p>文件大小：{$fileSize}</p>
                    <p>shell_exec：{$message}</p>
                    <p>保存路径：{$res['save_path']}</p>
                    <p>文件名称：{$res['file_name']}</p>
                    <p>部署成功</p>";
        } else {
            return '压缩包下载失败';
        }
    }
    public function getFile($url, $save_dir = '', $filename = '', $type = 0)
    {
        if (trim($url) == '') {
            return false;
        }
        if (trim($save_dir) == '') {
            $save_dir = './';
        }
        if (0 !== strrpos($save_dir, '/')) {
            $save_dir .= '/';
        }
        //创建保存目录
        if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
            return false;
        }
        $content = null;
        //获取远程文件所采用的方法
        if ($type) {
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $content = curl_exec($ch);

            // return $content->getClientOriginalName();
            curl_close($ch);
        } else {
            ob_start();
            readfile($url);
            $content = ob_get_contents();
            ob_end_clean();
        }
        $size = strlen($content);
        //文件大小  覆盖原文件
        $fp2 = fopen($save_dir . $filename, 'w');
        // if (!is_file($save_dir . $filename)) {
        //     //文件不存在需要写入文件
        //     fwrite($fp2, $content);
        // }
        fwrite($fp2, $content);
        fclose($fp2);
        unset($content, $url);
        return array(
            'file_name' => $filename,
            'save_path' => $save_dir . $filename,
            'file_size' => $size
        );
    }
}
